﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bat_sym.SimulationLogic
{
    public class PresetManager
    {
        private const string _repo = "Presets";

        private ObservableCollection<string> _presets;

        public PresetManager()
        {
            if (!Directory.Exists(_repo))
            {
                Directory.CreateDirectory(_repo);
            }

            _presets = new ObservableCollection<string>();
            foreach (var f in Directory.GetFiles(_repo))
            {
                var file = new FileInfo(f);
                _presets.Add(file.Name);
            }
        }

        public void DeletePreset(string name)
        {
            if(File.Exists(string.Format("{0}/{1}", _repo, name)))
            {
                File.Delete(string.Format("{0}/{1}", _repo, name));
            }

            Presets.Remove(name);
        }

        public void SavePreset(ObservableCollection<ISimulationRule> rules, string name, int coops, int comps)
        {
            var filerows = new List<string>();
            filerows.Add(string.Format("{0} {1}", coops, comps));
            foreach (var r in rules)
            {
                var row = string.Format("{0}|{1}",r.RuleName, r.IsActive);
                foreach (bat_sym.Helpers.ParameterDictionary.Parameter p in r.Parameters)
                {
                    var param = string.Format("~{0}|{1}", p.Name, p.Value);
                    row += param;
                }
                filerows.Add(row);
            }

            File.WriteAllLines(string.Format("{0}/{1}", _repo, name), filerows);

            Presets.Add(name);
        }

        public dynamic LoadPreset(ObservableCollection<ISimulationRule> rules, string name)
        {
            var rows = File.ReadAllLines(string.Format("{0}/{1}", _repo, name));
            var extra = rows[0].Split(' ');
            dynamic result = new { Coops = int.Parse(extra[0]), Comps = int.Parse(extra[1]) };
            for(var index = 1;index<rows.Length;index++)
            {
                var r = rows[index];
                var parts = r.Split('~');
                var rulestr = parts[0].Split('|');
                var rulename = rulestr[0];
                var ruleactive = bool.Parse(rulestr[1]);
                var rule = rules.FirstOrDefault(x => x.RuleName.Equals(rulename));
                if (rule != null)
                {
                    rule.IsActive = ruleactive;
                    for (int i = 1; i < parts.Length; i++)
                    {
                        var paramstr = parts[i].Split('|');
                        var paramname = paramstr[0];
                        var paramvalue = double.Parse(paramstr[1]);
                        rule.Parameters[paramname] = paramvalue;
                    }
                }
            }

            return result;
        }

        public ObservableCollection<string> Presets 
        {
            get
            {
                return _presets;
            }
        }
    }
}
